<template>
  <div>
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="到底了！"
  @load="onLoad" 
  :immediate-check="false"
  >
      <van-cell
        v-for="data in datalist"
        :key="data.filmId"
        @click="handleChangePage(data.filmId)"
      >
        <img :src="data.poster" />
        <div>
          <div class="title">{{ data.name }}</div>
          <div class="content">
            <div :class="data.grade ? '' : 'hidden'">
              观众评分:<span style="color: orange">{{ data.grade }}</span>
            </div>
            <div class="actors">主演: {{ data.actors | actorsFilter }}</div>
            <div>{{ data.nation }} | {{ data.runtime }}分钟</div>
          </div>
        </div>
      </van-cell>
</van-list>
  </div>
</template>
<script>
import http from "@/util/http";
import Vue from "vue";

Vue.filter("actorsFilter", (data) => {
  if (data === undefined) return "暂无主演";
  return data.map((item) => item.name).join(" ");
});
export default {
  data() {
    return {
      datalist: [],
		loading: false,
		finished: false,
		current: 1,
		total: 0
    };
  },

  mounted() {
    http({
      url: `/gateway?cityId=${this.$store.state.cityId}&pageNum=1&pageSize=10&type=1&k=349429`,
      headers: {
        "X-Host": "mall.film-ticket.film.list",
      },
    }).then((res) => {
      // console.log(res.data.data.films);
      this.datalist = res.data.data.films;

		this.total = res.data.data.total
    });
  },

  methods: {
	onLoad() {
		if(this.datalist.length === this.total && this.total !== 0){
			this.finished = true
			return
		}
		this.current++
		    http({
      url: `/gateway?cityId=${this.$store.state.cityId}&pageNum=${this.current}&pageSize=10&type=1&k=349429`,
      headers: {
        "X-Host": "mall.film-ticket.film.list",
      },
    }).then((res) => {
      // console.log(res.data.data.films);
      this.datalist = [...this.datalist,...res.data.data.films]

		// loading主动设置成 floaty
		this.loading = false
    });
	},
	handleChangePage(id) {
      // console.log(id);
      //编程式导航
      //location.href = '#/detail'

      // 新写法
      this.$router.push(`/detail/${id}`);
	},
	},
};
</script>
<style lang="scss" scoped>
.van-list {
  .van-cell {
    overflow: hidden;
    padding: 0.9375rem;

    img {
      width: 4.125rem;
      float: left;
    }

    .title {
      font-size: 16px;
    }

    .content {
      font-size: 13px;
      color: gray;

      .actors {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 200px;
      }
    }
  }
}

.hidden {
  visibility: hidden;
}
</style>
